<!-- src/components/Logout.vue -->
<template>
  <div class="logout-container">
    <p class="warning-message">注销账号后不可恢复，请确认是否继续。</p>
    <div class="button-group">
      <el-button type="danger" @click="dialogVisible = true">确认注销</el-button>
      <el-button type="primary" @click="goToHome">返回首页</el-button>
    </div>

    <el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <span>注销账号后不可恢复，请确认是否继续。</span>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="confirmLogout()">确 定</el-button>
  </span>
</el-dialog>

  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'Logout',
  data() {
      return {
        dialogVisible: false
      };
    },
  methods: {
    handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },

    confirmLogout() {

      let userId = localStorage.getItem("userId")
     axios.get("/api/teacher/logOut?userId="+userId).then(response => {
        this.dialogVisible = false

        localStorage.removeItem('userId');
        this.$router.push({ name: 'Login' });
     }).catch(err => {
        this.$message.error(err.response.data)
     })
    },
    goToHome() {
      this.$router.push({ name: 'StudentHome' });
    }
  }
};
</script>

<style scoped>
.logout-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 70vh;
}

.warning-message {
  font-weight: bold;
  font-size: 1.2em;
  color: red;
  margin-bottom: 20px;
}

.button-group {
  display: flex;
  gap: 20px; /* 按钮之间的间距 */
}
</style>
